﻿#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Linq;
#endregion

namespace VisualWebGuiApplication2
{
    public partial class Accompanying_stocks_open : UserControl
    {
        Accompanying_stocks selected;
        Configdb dbconf;
       
        Gizmox.WebGUI.Forms.BindingSource bindingSource1;
        public Accompanying_stocks_open()
        {
            InitializeComponent();
          
            //this.bindingSource1 = new Gizmox.WebGUI.Forms.BindingSource(this.components);
            //this.bindingSource1.DataMember = "Title";
            // this.bindingSource1.DataSource =GetAccompanying_stocks(); 
            // //this.comboBox1.DisplayMember = "Title";
            // comboBox1.DataSource = this.bindingSource1;
           // this.comboBox1.ValueMember = "Id";


            using (GazOnDevEntities db = new GazOnDevEntities())
            {


                dbconf = db.Configdbs.FirstOrDefault();
                textmakat.Text = dbconf.AcompanionStock;
                // must icrease id of invoice id  ;
            }
            button1.Enabled = false;
            txteditmakat.Leave += new EventHandler(txteditmakat_Leave);
            textmakat.Leave += new EventHandler(txteditmakat_Leave1);
        }


        public Accompanying_stocks_open(string selectedid)
        {
            InitializeComponent();

            //this.bindingSource1 = new Gizmox.WebGUI.Forms.BindingSource(this.components);
            //this.bindingSource1.DataMember = "Title";
            // this.bindingSource1.DataSource =GetAccompanying_stocks(); 
            // //this.comboBox1.DisplayMember = "Title";
            // comboBox1.DataSource = this.bindingSource1;
            // this.comboBox1.ValueMember = "Id";



            button1.Enabled = false;
            txteditmakat.Leave += new EventHandler(txteditmakat_Leave);
            textmakat.Leave += new EventHandler(txteditmakat_Leave1);
            txteditmakat.Text = selectedid;
            EventArgs ee = new EventArgs();
            txteditmakat_Leave(txteditmakat, ee);
         
        }

        public Accompanying_stocks_open(string selectedid, string logid)
        {
            InitializeComponent();

            //this.bindingSource1 = new Gizmox.WebGUI.Forms.BindingSource(this.components);
            //this.bindingSource1.DataMember = "Title";
            // this.bindingSource1.DataSource =GetAccompanying_stocks(); 
            // //this.comboBox1.DisplayMember = "Title";
            // comboBox1.DataSource = this.bindingSource1;
            // this.comboBox1.ValueMember = "Id";



            button1.Enabled = false;
            txteditmakat.Leave += new EventHandler(txteditmakat_Leave);
            textmakat.Leave += new EventHandler(txteditmakat_Leave1);
          
            //txteditmakat
            int logidsel = Convert.ToInt16(selectedid);
                
            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                Accompanying_stocks_log _log = db.Accompanying_stocks_log.FirstOrDefault(x => x.Id == logidsel);


               
                    
                  txteditmakat.Text = _log.makat;
                  EventArgs ee = new EventArgs();
                  txteditmakat_Leave(txteditmakat, ee);

                  txteditAmountCurrent.Text = _log.Count;
                  // newrecord.Title = txtStockTitle.Text;
                  txteditTitle.Text = _log.Title;
                  //newrecord.makat = textmakat.Text;
                  textmakat.Text = _log.makat;
                  txteditunit.Text = _log.Unit;
                  //newrecord.Unit = txtStockunit.Text;
                  txteditsuppler.Text = _log.supplier;
                  txteditdate.Text = _log.date;
                  txteditinvoiceid.Text = _log.invoicenumber;
                

            }

        }




        public List<Accompanying_stocks> GetAccompanying_stocks()
        {
            List<Accompanying_stocks> _Accompanying_stocks = new List<Accompanying_stocks>();

            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                var query = from p in db.Accompanying_stocks select p;
                _Accompanying_stocks = query.ToList<Accompanying_stocks>();


            }
            return _Accompanying_stocks;
        }







        private void btnOpenTank_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("הוספת פריט למלאי נלווה");
            using (GazOnDevEntities db = new GazOnDevEntities())
            {

                Accompanying_stocks temp = db.Accompanying_stocks.SingleOrDefault(x => x.makat == textmakat.Text);
                if (null !=temp)
                {
                    MessageBox.Show("מקט קיים נא לעבור למצב עריכה  ");
                    return;
                }
                Accompanying_stocks_log _log = new Accompanying_stocks_log();
                Accompanying_stocks newrecord = new Accompanying_stocks();
                newrecord.Count = txtStockAmount.Text;
                _log.Count = txtStockAmount.Text;
                newrecord.Title = txtStockTitle.Text;
                _log.Title = txtStockTitle.Text;
                newrecord.makat = textmakat.Text;
                _log.makat = textmakat.Text;
                _log.Unit = txtStockunit.Text;
                newrecord.Unit = txtStockunit.Text;
                _log.supplier = txtSupplliaer.Text;
                _log.date = txtDate.Text;
                _log.invoicenumber = txtInvoiceNumber.Text;

                db.AddToAccompanying_stocks(newrecord);
                db.AddToAccompanying_stocks_log(_log);
                try
                {
                    dbconf.AcompanionStock = (Convert.ToInt32(dbconf.AcompanionStock) + 1).ToString();
                    db.Attach(dbconf);
                    db.ObjectStateManager.ChangeObjectState(dbconf, EntityState.Modified);
                }
                catch { }

                int result = db.SaveChanges();
                if (result > 0)
                {
                    MessageBox.Show("פריט התווסף למלאי בהצלחה " + result);
                 
                }


            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // update  

            double newamount = Convert.ToDouble(txteditAmount.Text) + Convert.ToDouble(txteditAmountCurrent.Text);
              using (GazOnDevEntities db = new GazOnDevEntities())
            {
                selected = db.Accompanying_stocks.FirstOrDefault(x => x.makat == txteditmakat.Text);
                //  comboBox1.sele = selected.
                //comboBox1.FindStringExact(selected.Title)
               // x.SelectedIndex = x.FindStringExact(selected.Title);
               // txteditTitle.Text = selected.Title;
              }
              if (null != selected)
              {

                  selected.Count = string.Format("{0}", newamount);
                  selected.Title = txteditTitle.Text;
                  using (GazOnDevEntities db = new GazOnDevEntities())
                  {

                      Accompanying_stocks_log _log = new Accompanying_stocks_log();

                      _log.Count = txteditAmount.Text;

                      _log.Title = txteditTitle.Text;

                      _log.makat = txteditmakat.Text;
                      _log.Unit = txteditunit.Text;

                      _log.supplier = txteditsuppler.Text;
                      _log.date = txteditdate.Text;
                      _log.invoicenumber = txteditinvoiceid.Text;

                      db.AddToAccompanying_stocks_log(_log);
                      db.Attach(selected);
                      db.ObjectStateManager.ChangeObjectState(selected, EntityState.Modified);
                      db.SaveChanges();
                      MessageBox.Show("נשמר בהצלחה ");
                      txteditAmount.Text = "";
                      //comboBox1.SelectedIndex = comboBox1.Items.IndexOf(selected);
                      //   x.SelectedIndex = x.FindStringExact(selected.Title);
                  }
              }
            button1.Enabled = false;
        }

       
        void txteditmakat_Leave(object sender, EventArgs e)
        {
            //txteditmakat
            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                selected = db.Accompanying_stocks.FirstOrDefault(x => x.makat == txteditmakat.Text);
                //  comboBox1.sele = selected.
                //comboBox1.FindStringExact(selected.Title)
               // x.SelectedIndex = x.FindStringExact(selected.Title);
               // txteditTitle.Text = selected.Title;

                if (null != selected)
                {
                    MessageBox.Show("הפריט קיים במלאי אתה מועבר למצב עריכה");
                    txteditTitle.Text = selected.Title;

                    txteditmakat.Text = selected.makat;

                    //txteditAmount.Text = selected.Count;
                    txteditAmountCurrent.Text = selected.Count;
                    button1.Enabled = true;
                }


            }

        }
        void txteditmakat_Leave1(object sender, EventArgs e)
        {
            
            //txteditmakat
            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                selected = db.Accompanying_stocks.FirstOrDefault(x => x.makat == textmakat.Text);
                //  comboBox1.sele = selected.
                //comboBox1.FindStringExact(selected.Title)
                if (null != selected)
                {
                    
               
                //if (comboBox1.FindCode(selected.Id.ToString()) >0 )
                //{
                    //comboBox1.FindCode(s.find .SelectedIndex = selected.Id;// comboBox1.FindStringExact(selected.Title);
                    //comboBox1.FindCode(selected.Id.ToString());
                    //comboBox1.SelectedItem = selected;
                    MessageBox.Show("הפריט קיים במלאי אתה מועבר למצב עריכה");
                    txteditTitle.Text = selected.Title;
                   
                    txteditmakat.Text = selected.makat;

                    //txteditAmount.Text = selected.Count;
                    txteditAmountCurrent.Text = selected.Count;
                    button1.Enabled = true;
                    // CurrentAmount = selected.Count;
                    /*MessageBox.Show(selcted.Title);
                    MessageBox.Show(selcted.Id.ToString());*/
                //}
                }
               // comboBox1.Items


            }
        }

    }
        
    }
